package com.zwl.sjzmanagement.controller;

import com.atguigu.tingshu.common.result.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwl.model.ExpansionBox;
import com.zwl.model.Firearms;
import com.zwl.sjzmanagement.service.FirearmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/firearms")
public class FirearmsController {

    @Autowired
    private FirearmsService firearmsService;

    //http://localhost:8002/api/firearms/firearmsList/{pageNo}/{pageNum}
    @GetMapping("/firearmsList")
    public Result getFirearmsList(@RequestParam(defaultValue = "1") Long pn, @RequestParam(defaultValue = "10") Long pz) {
        return Result.ok(firearmsService.page(new Page<>(pn, pz))) ;
    }


//    http://localhost:8001/api/firearms/firearmsAddOrUpdate
    @PostMapping("/firearmsAddOrUpdate")
    public Result firearmsAddOrUpdate(@RequestBody Firearms firearms ) {

        firearmsService.saveOrUpdate(firearms);

        return Result.ok();
    }

//    http://localhost:8001/api/firearms/firearmsDelById

    @DeleteMapping("/firearmsDelById/{id}")
    public Result firearmsDelById(@PathVariable Integer id) {

        firearmsService.removeById(id);

        return Result.ok();

    }

    @PostMapping("/firearmsBatchDelById")
    public Result firearmsBatchDelById(@RequestBody List<Integer> ids) {

        //根据id判断，相同则执行跟新操作
        firearmsService.removeBatchByIds(ids);

        return Result.ok();

    }

    @GetMapping("/getFirearmsById/{id}")
    public Result getFirearmsById(@PathVariable Long id) {

        Firearms firearms = firearmsService.getById(id);
        return Result.ok(firearms);

    }

}
